//
//  HMVisitorView.swift
//  Weibo
//
//  Created by 李学林 on 15/12/6.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit
import SnapKit

class HMVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
            setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 给外界提供一个方法用来设置访客界面的视图
    func setVisitorInfo(imageName: String?, message:String?){
    
        if let img = imageName, mesg = message {
            
            circleView.hidden = true
            iconView.image = UIImage(named: img)
            messageLabel.text = mesg
        
        }else{
            startAnimation();
        }
    }

    
    
    // 添加动画 私有的方法
    private func startAnimation(){
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    
    
    private func setupUI(){
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
    // 添加子视图
        addSubview(circleView)
        addSubview(shadowView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(loginButton)
 ///  给子视图添加约束
        // 2.1 图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        // 2.2 圆圈
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        // 2.3
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.centerX.equalTo(circleView)
            make.width.equalTo(224)
        }
        // 2.4 注册按钮
        registButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        // 2.5 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registButton)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        // 2.5 透明阴影
        shadowView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registButton.snp_bottom)
        }
        
//        //iconView
//       iconView.snp_makeConstraints { (make) -> Void in
//        make.center.equalTo(self.snp_center);
//        }
//        
//        //circleView
//        circleView.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(iconView.snp_center)
//        }
//        
//        //messageLabel
//        messageLabel.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(circleView)
//            make.top.equalTo(circleView.snp_bottom).offset(16)
//            make.width.equalTo(circleView)
//        }
//        
//       
//        
//        //注册按钮
//        //registButton
//        registButton.snp_makeConstraints { (make) -> Void in
//            make.leading.equalTo(messageLabel)
//            make.top.equalTo(messageLabel.snp_bottom).offset(16)
//            make.size.equalTo(CGSizeMake(100, 35))
//            
//        }
//        
//        //loginButton
//        loginButton.snp_makeConstraints { (make) -> Void in
//            
//            make.trailing.equalTo(messageLabel)
//            make.top.equalTo(registButton)
//            make.size.equalTo(CGSizeMake(100, 35))
//            
//        }
//        
//        //shadowView
//        shadowView.snp_makeConstraints { (make) -> Void in
//            make.width.equalTo(self)
//            make.bottom.equalTo(loginButton)
//            make.top.equalTo(self)
//        }
        
    }
    
    /// MARK 懒加载
    // 小房子视图
    lazy var iconView: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_image_house")
        
        let imageView = UIImageView(image: image)
        
        return imageView
    }()
    
    // 圆圈
    lazy var circleView: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
    
        let circleView = UIImageView(image: image)
        
        return circleView
    
    }()
    
    // 消息
    lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        
        // 注意:设置文字的颜色的代码 必须写在设置文字之前,要不就会报错
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0;
        
        return label
    
    }()
    
    // 注册按钮
    lazy var registButton : UIButton = {
    
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)

        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    
    }()
    
    // 登录按钮
    lazy var loginButton: UIButton = {
    
        let button = UIButton()
        // 设置文字以及文字颜色
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        // 设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        // 设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    
    }()
    
    // 阴影
    lazy var shadowView: UIImageView = {
    
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        let imageView = UIImageView(image: image)
        return imageView
    }()
    
    func demo(){
    
        //iconView
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy:.Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //circleView
        circleView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //messageLabel
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        // 宽度
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        // 距离上边控件距离
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        
        // 2.4 注册按钮
        registButton.translatesAutoresizingMaskIntoConstraints = false
        
        registButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        // 约束宽度以及高度
        addConstraint(NSLayoutConstraint(item: registButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        // 2.5 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: .Top, multiplier: 1, constant: 0))
        // 约束宽度以及高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        
        shadowView.translatesAutoresizingMaskIntoConstraints = false
        
        shadowView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[shadowView]-0-|", options: [], metrics: nil, views: ["shadowView": shadowView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[shadowView]-(offset)-[registerButton]", options: [], metrics: ["offset" : -35], views: ["shadowView": shadowView,"registerButton": registButton]))

    
    
    
    
    }
    
    
}
